using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;


namespace YATC
{
        public enum CellStatus
        {
            Buzy, Free, Frozed,Rotate,Moving,FMoving
        }
    /// <summary>
    /// This is a game component that implements IUpdateable.
    /// </summary>
    public class Cell : DrawableGameComponent
    {
        private readonly Model model;
        private CellStatus status;
        private readonly float scale;
        private Matrix gameWorldRotation;
        private Vector3 position,__position;

        private float RotationX;
        private float py;
        private Texture2D[] textures;
        private int col;
        private Texture2D texture;
        private float z;
        private TimeSpan mTime = TimeSpan.Zero;

        public Cell(Game game, Model model, float scale, Vector3 position, Texture2D[] textures)
            : base(game)
        {
            this.model = model;
            this.textures = textures;
            this.position = position;
            this.scale = scale;
           
        }
        public override void Initialize()
        {
            col = -1;
            status = CellStatus.Free;
            RotationX = 0.0f;
            py = position.Y;
            z = position.Z;
            __position = position;

            base.Initialize();
        }
        public CellStatus Status
        {
            get{ return status;}
            set{ status = value;}
        }
        public int CoLor
        {
            get { return col; }
            set { col = value; }
        }
        /// <summary>
        /// Allows the game component to update itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        public override void Update(GameTime gameTime)
        {

            if (status == CellStatus.Rotate)
            {
                RotationX -= 1.3f;
            }
            else
                RotationX = 0;

            gameWorldRotation =
               Matrix.CreateRotationX(MathHelper.ToRadians(RotationX));


            if (status == CellStatus.Moving)
            {
                mTime += gameTime.ElapsedGameTime;
                if (mTime > TimeSpan.FromMilliseconds(100))
                {
                    Move();
                    mTime -= TimeSpan.FromMilliseconds(100);
                }
            }

            base.Update(gameTime);
        }

        private void Move()
        {
           position.Z += 0.8f;
            if (position.Z > 20)
            {
                position = __position;
                status = CellStatus.FMoving;          
            }
        }

        public override void Draw(GameTime gameTime)
        {

            if (status != CellStatus.Free && col > -1 || status == CellStatus.Rotate)
            {
                DrawModel(model);
            }
            base.Draw(gameTime);
        }

        private void DrawModel(Model m)
        {
            if (col > -1)
                texture = textures[col];

            var transforms = new Matrix[m.Bones.Count];
            m.CopyAbsoluteBoneTransformsTo(transforms);

            foreach (ModelMesh mesh in m.Meshes)
            {
                foreach (BasicEffect effect in mesh.Effects)
                {
                    effect.EnableDefaultLighting();


                   effect.TextureEnabled = true;
                    effect.Texture = texture;
  

                    effect.View = ((Game1) Game).camera.ViewMatrix;
                    effect.Projection = ((Game1) Game).camera.projectionMatrix;
                    effect.World = gameWorldRotation*
                                   transforms[mesh.ParentBone.Index]*
                                   Matrix.CreateTranslation(position)*Matrix.CreateScale(scale);
                }
                mesh.Draw();
            }
        }
      

        
    }
}